import moment from "moment";


export function formatTimeByMoment(date, format = 'YYYY-MM-DD HH:mm:ss') {
    if (typeof date === 'number' && date.toString().length === 10) {
        return moment.unix(date).format(format);
    }
    return moment(date).format(format);
}

export function formatTime(date, format = 'YYYY-MM-DD HH:mm:ss') {
    if (typeof date === 'number' && date.toString().length === 10) {
        return moment.unix(date).format(format);
    }
    return moment(date).format(format);
}

/**
 * 通过最近时间和单位（最近一周，最近一月等），获取时间段（时间戳数组）=>时间前推
 * @param {*} timeValue 最近时间
 * @param {*} timeUnit  时间单位
 * @param {*} delay 延迟毫秒数
 */
export function getTimeRangeByUnit(timeValue, timeUnit, delay = 0) {
    let rangeArr = [];

    if (!timeValue || !timeUnit || timeValue <= 0) return rangeArr;

    const start = new Date().getTime() - delay;
    const end = new Date().getTime() - delay;

    switch (timeUnit) {
        case "second":
            rangeArr = [start - 1000 * timeValue, end];
            break;
        case "minute":
            rangeArr = [start - 60 * 1000 * timeValue, end];
            break;
        case "hour":
            rangeArr = [start - 3600 * 1000 * timeValue, end];
            break;
        case "day":
            rangeArr = [start - 3600 * 1000 * 24 * timeValue, end];
            break;
        case "week":
            rangeArr = [start - 3600 * 1000 * 24 * 7 * timeValue, end];
            break;
        case "month":
            rangeArr = [start - 3600 * 1000 * 24 * 30 * timeValue, end];
            break;
        case "year":
            rangeArr = [start - 3600 * 1000 * 24 * 365 * timeValue, end];
            break;
    }
    return rangeArr;
}